﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;

namespace B2B.Web
{
    public class EMail
    {
        public static string EmailSend(string kimden, string kime, string kullaniciAdi, string sifre, string konu, string icerik, string sunucuAdi, string atach_file)
        {
            try
            {
                System.Web.Mail.MailMessage objMail = new System.Web.Mail.MailMessage();
                objMail.To = kime;
                objMail.From = "\\" + kimden + "\\ <" + kimden + ">";
                objMail.Subject = konu;
                objMail.Body = icerik;
                objMail.BodyFormat = System.Web.Mail.MailFormat.Html;
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtsperver", sunucuAdi);
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "587");
                //objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true")
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", kullaniciAdi);
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sifre);
                if (!string.IsNullOrEmpty(atach_file))
                {
                    objMail.Attachments.Add(new System.Web.Mail.MailAttachment(atach_file));
                }
                System.Web.Mail.SmtpMail.SmtpServer = sunucuAdi;
                System.Web.Mail.SmtpMail.Send(objMail);
                return "OK";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public static string EmailSend2(string kimden, string kime, string kullaniciAdi, string sifre, string konu, string icerik, string sunucuAdi, string atach_file)
        {
            try
            {
                System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
                System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient();
                m.From = new System.Net.Mail.MailAddress(kimden, "b2b.ofisboss.com");
                m.To.Add(new System.Net.Mail.MailAddress(kime, "b2b.ofisboss.com"));
                m.Subject = konu;
                m.Body = icerik;

                System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.Default.GetBytes(icerik));
                System.Net.Mail.Attachment a = new System.Net.Mail.Attachment(ms, "siparis.html", System.Net.Mime.MediaTypeNames.Text.Html);
                m.Attachments.Add(a);

                string str = "<html><body><h1>" + konu + "</h1><br/><a href=\"cid:siparis\">Siparis'i görüntülemek için tıklayın</a></body></html>";
                System.Net.Mail.AlternateView av = System.Net.Mail.AlternateView.CreateAlternateViewFromString(str, null, System.Net.Mime.MediaTypeNames.Text.Html);
                System.Net.Mail.LinkedResource lr = new System.Net.Mail.LinkedResource(ms, System.Net.Mime.MediaTypeNames.Text.Html);
                lr.ContentId = "siparis";
                av.LinkedResources.Add(lr);
                m.AlternateViews.Add(av);

                sc.Host = sunucuAdi;
                sc.Port = 587;
                sc.Credentials = new System.Net.NetworkCredential(kullaniciAdi, sifre);
                sc.Send(m);
                return "OK";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }


    [DataContract]
    public class KullaniciGrup
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string Adi { get; set; }
    }

    [DataContract]
    public class Parametre
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string ParametreAdi { get; set; }
        [DataMember]
        public string ParametreDegeri { get; set; }
    }

    [DataContract]
    public class SiparisOzet
    {
        [DataMember]
        public bool Sec { get; set; }
        [DataMember]
        public DateTime Tarih { get; set; }
        [DataMember]
        public string Seri { get; set; }
        [DataMember]
        public int Sira { get; set; }
        [DataMember]
        public string Kullanici { get; set; }
        [DataMember]
        public double Tutar { get; set; }
        [DataMember]
        public string Durum { get; set; }
    }

    [DataContract]
    public class SiparisDetay
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public bool Sec { get; set; }
        [DataMember]
        public string Kod { get; set; }
        [DataMember]
        public string Adi { get; set; }
        [DataMember]
        public double Adet { get; set; }
        [DataMember]
        public double Fiyat { get; set; }
        [DataMember]
        public double Iskonto { get; set; }
        [DataMember]
        public double KdvOrani { get; set; }
        [DataMember]
        public double KdvTutari { get; set; }
        [DataMember]
        public double Yekun { get; set; }
    }

    [DataContract]
    public class Cari
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string Kodu { get; set; }
        [DataMember]
        public string Adi { get; set; }
    }

    [DataContract]
    public class DovizKuru
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public byte DovizCinsi { get; set; }
        [DataMember]
        public double Kur { get; set; }
    }

    [DataContract]
    public class Favorite
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public int KullaniciID { get; set; }
        [DataMember]
        public int StokID { get; set; }
    }

    [DataContract]
    public class FiyatListesi
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string CariKodu { get; set; }
        [DataMember]
        public string StokKodu { get; set; }
        [DataMember]
        public double BrutFiyat { get; set; }
        [DataMember]
        public double Isk1Oran { get; set; }
        [DataMember]
        public double Isk2Oran { get; set; }
        [DataMember]
        public double Isk3Oran { get; set; }
        [DataMember]
        public double Isk4Oran { get; set; }
        [DataMember]
        public double Isk5Oran { get; set; }
        [DataMember]
        public double Isk6Oran { get; set; }
        [DataMember]
        public double Isk1Tutar { get; set; }
        [DataMember]
        public double Isk2Tutar { get; set; }
        [DataMember]
        public double Isk3Tutar { get; set; }
        [DataMember]
        public double Isk4Tutar { get; set; }
        [DataMember]
        public double Isk5Tutar { get; set; }
        [DataMember]
        public double Isk6Tutar { get; set; }
        [DataMember]
        public byte DovizCinsi { get; set; }
    }

    [DataContract]
    public class DbKategori
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string Kodu { get; set; }
        [DataMember]
        public string Adi { get; set; }
    }

    [DataContract]
    public class DbAltGrup
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string Kodu { get; set; }
        [DataMember]
        public string Adi { get; set; }
    }

    [DataContract]
    public class AltGrup
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string Kodu { get; set; }
        [DataMember]
        public string Adi { get; set; }
        [DataMember]
        public int StokSayisi { get; set; }
    }

    [DataContract]
    public class Kategori
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string Kodu { get; set; }
        [DataMember]
        public string Adi { get; set; }
        [DataMember]
        public int StokSayisi { get; set; }
    }

    [DataContract]
    public class Kullanici
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string FirmaKodu { get; set; }
        [DataMember]
        public string Email { get; set; }
        [DataMember]
        public string Sifre { get; set; }
        [DataMember]
        public bool SiparisVerebilir { get; set; }
        [DataMember]
        public bool OnayVerebilir { get; set; }
        [DataMember]
        public bool SiparisiOnayli { get; set; }
        [DataMember]
        public string Adi { get; set; }
        [DataMember]
        public string Soyadi { get; set; }
        [DataMember]
        public string Telefon { get; set; }
        [DataMember]
        public string CepTelefonu { get; set; }
        [DataMember]
        public string Adres { get; set; }
        [DataMember]
        public string Sehir { get; set; }
        [DataMember]
        public string Ilce { get; set; }
        [DataMember]
        public DateTime SonGirisTarihi { get; set; }
        [DataMember]
        public string SonGirisIPAdress { get; set; }
        [DataMember]
        public bool ListeDisiAlabilir { get; set; }
        [DataMember]
        public bool Yonetici { get; set; }
        [DataMember]
        public int GrupID { get; set; }
    }

    [DataContract]
    public class Siparis
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public DateTime Tarih { get; set; }
        [DataMember]
        public int KullaniciID { get; set; }
        [DataMember]
        public string Seri { get; set; }
        [DataMember]
        public int Sira { get; set; }
        [DataMember]
        public int Satir { get; set; }
        [DataMember]
        public string CariKodu { get; set; }
        [DataMember]
        public string StokKodu { get; set; }
        [DataMember]
        public double Fiyat { get; set; }
        [DataMember]
        public double Miktar { get; set; }
        [DataMember]
        public double Iskonto1 { get; set; }
        [DataMember]
        public double Iskonto2 { get; set; }
        [DataMember]
        public double Iskonto3 { get; set; }
        [DataMember]
        public double Iskonto4 { get; set; }
        [DataMember]
        public double Iskonto5 { get; set; }
        [DataMember]
        public double Iskonto6 { get; set; }
        [DataMember]
        public int KdvID { get; set; }
        [DataMember]
        public double KdvOran { get; set; }
        [DataMember]
        public double KdvTutar { get; set; }
        [DataMember]
        public double Tutar { get; set; }
        [DataMember]
        public bool Onaylandi { get; set; }
        [DataMember]
        public int OnaylayanKullaniciID { get; set; }
        [DataMember]
        public string Aciklama { get; set; }
        [DataMember]
        public bool Gonderildi { get; set; }
    }

    [DataContract]
    public class Stok
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string Kodu { get; set; }
        [DataMember]
        public string Adi { get; set; }
        [DataMember]
        public int KdvID { get; set; }
        [DataMember]
        public double KdvOrani { get; set; }
        [DataMember]
        public byte[] Resim { get; set; }
        [DataMember]
        public string KategoriKodu { get; set; }
        [DataMember]
        public byte DovizCinsi { get; set; }
        [DataMember]
        public double Fiyat { get; set; }
        [DataMember]
        public string AltGrupKodu { get; set; }
    }

    public class B2bEntities : DbContext
    {
        public DbSet<Cari> Cariler { get; set; }
        public DbSet<DovizKuru> DovizKurlari { get; set; }
        public DbSet<Favorite> Favorites { get; set; }
        public DbSet<FiyatListesi> FiyatListeleri { get; set; }
        public DbSet<DbKategori> Kategoriler { get; set; }
        public DbSet<DbAltGrup> AltGruplar { get; set; }
        public DbSet<Kullanici> Kullanicilar { get; set; }
        public DbSet<Siparis> Siparisler { get; set; }
        public DbSet<Stok> Stoklar { get; set; }
        public DbSet<Parametre> Parametreler { get; set; }
        public DbSet<KullaniciGrup> KullaniciGruplari { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Cari>().ToTable("Cari");
            modelBuilder.Entity<DovizKuru>().ToTable("DovizKuru");
            modelBuilder.Entity<Favorite>().ToTable("Favorite");
            modelBuilder.Entity<FiyatListesi>().ToTable("FiyatListesi");
            modelBuilder.Entity<DbKategori>().ToTable("Kategori");
            modelBuilder.Entity<DbAltGrup>().ToTable("AltGrup");
            modelBuilder.Entity<Kullanici>().ToTable("Kullanici");
            modelBuilder.Entity<Siparis>().ToTable("Siparis");
            modelBuilder.Entity<Stok>().ToTable("Stok");
            modelBuilder.Entity<Parametre>().ToTable("SistemParametreleri");
            modelBuilder.Entity<KullaniciGrup>().ToTable("KullaniciGrup");

            modelBuilder.Entity<Cari>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            modelBuilder.Entity<Cari>().HasKey(x => x.Kodu);

            modelBuilder.Entity<Parametre>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<Parametre>().HasKey(x => x.ParametreAdi);

            //modelBuilder.Entity<DovizKuru>().Property(x => x.Kur).HasColumnType("float");
            modelBuilder.Entity<DovizKuru>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<DovizKuru>().HasKey(x => x.DovizCinsi);

            modelBuilder.Entity<Favorite>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<Favorite>().HasKey(x => new { x.KullaniciID, x.StokID });

            modelBuilder.Entity<FiyatListesi>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            modelBuilder.Entity<FiyatListesi>().HasKey(x => new { x.CariKodu, x.StokKodu });

            modelBuilder.Entity<DbKategori>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            modelBuilder.Entity<DbKategori>().HasKey(x => x.Kodu);

            modelBuilder.Entity<DbAltGrup>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            modelBuilder.Entity<DbAltGrup>().HasKey(x => x.Kodu);

            modelBuilder.Entity<Kullanici>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<Kullanici>().HasKey(x => x.Email);

            modelBuilder.Entity<Siparis>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            modelBuilder.Entity<Siparis>().HasKey(x => new { x.Seri, x.Sira, x.Satir });

            modelBuilder.Entity<Stok>().Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            modelBuilder.Entity<Stok>().HasKey(x => x.Kodu);

            //modelBuilder.Entity<FiyatListesi>().Property(x => x.BrutFiyat).HasColumnType("float");
            //modelBuilder.Entity<FiyatListesi>().Property(x => x.Isk1Oran).HasColumnType("float");
            //modelBuilder.Entity<FiyatListesi>().Property(x => x.Isk2Oran).HasColumnType("float");
            //modelBuilder.Entity<FiyatListesi>().Property(x => x.Isk3Oran).HasColumnType("float");
            //modelBuilder.Entity<FiyatListesi>().Property(x => x.Isk4Oran).HasColumnType("float");
            //modelBuilder.Entity<FiyatListesi>().Property(x => x.Isk5Oran).HasColumnType("float");
            //modelBuilder.Entity<FiyatListesi>().Property(x => x.Isk6Oran).HasColumnType("float");
            //modelBuilder.Entity<FiyatListesi>().Property(x => x.Isk1Tutar).HasColumnType("float");
            //modelBuilder.Entity<FiyatListesi>().Property(x => x.Isk2Tutar).HasColumnType("float");
            //modelBuilder.Entity<FiyatListesi>().Property(x => x.Isk3Tutar).HasColumnType("float");
            //modelBuilder.Entity<FiyatListesi>().Property(x => x.Isk4Tutar).HasColumnType("float");
            //modelBuilder.Entity<FiyatListesi>().Property(x => x.Isk5Tutar).HasColumnType("float");
            //modelBuilder.Entity<FiyatListesi>().Property(x => x.Isk6Tutar).HasColumnType("float");

            //modelBuilder.Entity<Siparis>().Property(x => x.Fiyat).HasColumnType("float");
            //modelBuilder.Entity<Siparis>().Property(x => x.Miktar).HasColumnType("float");
            //modelBuilder.Entity<Siparis>().Property(x => x.Iskonto1).HasColumnType("float");
            //modelBuilder.Entity<Siparis>().Property(x => x.Iskonto2).HasColumnType("float");
            //modelBuilder.Entity<Siparis>().Property(x => x.Iskonto3).HasColumnType("float");
            //modelBuilder.Entity<Siparis>().Property(x => x.Iskonto4).HasColumnType("float");
            //modelBuilder.Entity<Siparis>().Property(x => x.Iskonto5).HasColumnType("float");
            //modelBuilder.Entity<Siparis>().Property(x => x.Iskonto6).HasColumnType("float");
            //modelBuilder.Entity<Siparis>().Property(x => x.KdvOran).HasColumnType("float");
            //modelBuilder.Entity<Siparis>().Property(x => x.KdvTutar).HasColumnType("float");
            //modelBuilder.Entity<Siparis>().Property(x => x.Tutar).HasColumnType("float");

            modelBuilder.Entity<Stok>().Property(x => x.Resim).HasColumnType("image");
            modelBuilder.Entity<Stok>().Property(c => c.Resim).HasMaxLength(int.MaxValue);
            //modelBuilder.Entity<Stok>().Property(x => x.KdvOrani).HasColumnType("float");
        }
    }
}